package algotrader.book;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class Book {
	private String ativo;
	private Set<BookLine> compraLines;
	private Set<BookLine> vendaLines;
	
	public Book(String ativo) {
		this.ativo = ativo;
		this.compraLines = new LinkedHashSet<BookLine>();
		this.vendaLines = new LinkedHashSet<BookLine>();
	}
	
	public String getAtivo() {
		return ativo;
	}
	
	public List<BookLine> getVendaBookLines() {
		List<BookLine> aux = new LinkedList<BookLine>();
		Iterator<BookLine> i = vendaLines.iterator();
		while(i.hasNext()) aux.add(i.next());
		//ordenanda lista
		Collections.sort(aux,new BookLineComparator());
		return aux;
	}
	
	public List<BookLine> getCompraBookLines() {
		List<BookLine> aux = new LinkedList<BookLine>();
		Iterator<BookLine> i = compraLines.iterator();
		while(i.hasNext()) aux.add(i.next());
		//ordenanda lista
		Collections.sort(aux,new BookLineComparator());
		Collections.reverse(aux);
		return aux;
	}
	
	public int hashCode() {
		return ativo.hashCode();
	}
	
	public boolean equals(Object o) {
		Book b = (Book)o;
		return this.ativo.equals(b.getAtivo());
	}

	public void addBookLine(BookLine bookLine) {
		if(bookLine.getTipo()==BookLineType.COMPRA)
			compraLines.add(bookLine);
		else
			vendaLines.add(bookLine);
	}
	
	public void addBookLines(List<BookLine> bls) {
		if(bls.get(0).getTipo() == BookLineType.COMPRA)
			for(BookLine bl : bls)
				compraLines.add(bl);
		else
			for(BookLine bl : bls)
				vendaLines.add(bl);
	}

	public void removeBookLine(BookLine bookLine) {
		if(bookLine.getTipo()==BookLineType.COMPRA)
			compraLines.remove(bookLine);
		else
			vendaLines.remove(bookLine);
	}
}
